package com.alavente.dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.alavente.objects.DemandeAchat;

public class HibernateDemandeAchatDAO extends HibernateAbstractDAO<DemandeAchat> implements GenericDAOInterface<DemandeAchat> {

public DemandeAchat getById(int id) {
		
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		DemandeAchat object = null;
		
		try {
			transaction = session.beginTransaction();			
			object = (DemandeAchat) session.get(DemandeAchat.class, id);			
			transaction.commit();
		} 
		catch (HibernateException he) {
			he.printStackTrace();
			if(transaction != null) {
				try { transaction.rollback();} catch(HibernateException he2) {
					he2.printStackTrace(); }
				}
		}
		finally {
			if(session != null) {
				try { session.close();} catch(HibernateException he) {
					he.printStackTrace(); }
			}
		}
		
		return object;
	}

	public List<DemandeAchat> getDemandeAchatClient(int numClient) {

		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		List<DemandeAchat> list = new ArrayList<DemandeAchat>();
		
		try {
			transaction = session.beginTransaction();			
			list = session.createQuery("from DemandeAchat demandeAchat where demandeAchat.client = '" + numClient + "'").list();
			transaction.commit();
		} 
		catch (HibernateException he) {
			he.printStackTrace();
			if(transaction != null) {
				try { transaction.rollback();} catch(HibernateException he2) {
					he2.printStackTrace(); }
				}
		}
		finally {
			if(session != null) {
				try { session.close();} catch(HibernateException he) {
					he.printStackTrace(); }
			}
		}
		
		return list;
	}

	public List<DemandeAchat> getDemandeAchatCategorie(int numCategorie, String statut) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		List<DemandeAchat> list = new ArrayList<DemandeAchat>();
		
		try {
			transaction = session.beginTransaction();			
			list = session.createQuery("from DemandeAchat demandeAchat where demandeAchat.categorie = '" + numCategorie + 
					"' and demandeAchat.statut = '" + statut + "' order by demandeAchat.dateDepot desc").list();
			transaction.commit();
		} 
		catch (HibernateException he) {
			he.printStackTrace();
			if(transaction != null) {
				try { transaction.rollback();} catch(HibernateException he2) {
					he2.printStackTrace(); }
				}
		}
		finally {
			if(session != null) {
				try { session.close();} catch(HibernateException he) {
					he.printStackTrace(); }
			}
		}
		
		return list;
	}

	public List<DemandeAchat> getDemandeAchatSsCategorie(int numSsCategorie, String statut) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		List<DemandeAchat> list = new ArrayList<DemandeAchat>();
		
		try {
			transaction = session.beginTransaction();			
			list = session.createQuery("from DemandeAchat demandeAchat where demandeAchat.ssCategorie = '" + numSsCategorie +
					"' and demandeAchat.statut = '" + statut + "' order by demandeAchat.dateDepot desc").list();
			transaction.commit();
		} 
		catch (HibernateException he) {
			he.printStackTrace();
			if(transaction != null) {
				try { transaction.rollback();} catch(HibernateException he2) {
					he2.printStackTrace(); }
				}
		}
		finally {
			if(session != null) {
				try { session.close();} catch(HibernateException he) {
					he.printStackTrace(); }
			}
		}
		
		return list;
	}
	
	public List<DemandeAchat> getAllDemandeAchat(String statut) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		List<DemandeAchat> list = new ArrayList<DemandeAchat>();
		
		try {
			transaction = session.beginTransaction();			
			list = session.createQuery("from DemandeAchat demande where demande.statut = '" + statut + "' order by demande.dateDepot desc").list();
			transaction.commit();
		} 
		catch (HibernateException he) {
			he.printStackTrace();
			if(transaction != null) {
				try { transaction.rollback();} catch(HibernateException he2) {
					he2.printStackTrace(); }
				}
		}
		finally {
			if(session != null) {
				try { session.close();} catch(HibernateException he) {
					he.printStackTrace(); }
			}
		}
		
		return list;
	}

	public List<DemandeAchat> getDemandesAchat(List<Integer> listNumDemande) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		List<DemandeAchat> list = new ArrayList<DemandeAchat>();
		
		try {
			transaction = session.beginTransaction();
			StringBuilder query = new StringBuilder("from DemandeAchat demande where ");
			
			for (Integer numDemande : listNumDemande) {
				query.append("numDemande = " + numDemande + " OR ");
			}
			int length = query.length();
			query.delete(length-3, length-1);
			
			System.out.println(query.toString());
			
			list = session.createQuery(query.toString()).list();
			transaction.commit();
		} 
		catch (HibernateException he) {
			he.printStackTrace();
			if(transaction != null) {
				try { transaction.rollback();} catch(HibernateException he2) {
					he2.printStackTrace(); }
				}
		}
		finally {
			if(session != null) {
				try { session.close();} catch(HibernateException he) {
					he.printStackTrace(); }
			}
		}
		
		return list;
	}
}
